﻿using CommonTools;
using CommonTools.Controls;
using shop.Bll.cost;
using shop.Model;
using System;
using System.Linq;
using System.Windows.Forms;

namespace shop.cost
{
    public partial class BufaConfigForm : Form
    {
        private ShuadanConfigBll _bll = new ShuadanConfigBll();
        private int _id = 0;

        public BufaConfigForm()
        {
            InitializeComponent();
        }

        private async void ShuadanConfigForm_Load(object sender, EventArgs e)
        {
            var list = await _bll.QueryBufaItems();
            
            if(list.Any())
            {
                var model = list.First();
                this.box_bufaType.Text = model.Bufa;
                this.box_bufaPost.Value = model.BufaPost;
                _id = model.id;
            }
            else
            {
                this.box_bufaType.Text = "补发件专用";
            }
        }

        private async void btnok_Click(object sender, EventArgs e)
        {
            var model = new ShuadanConfig();
            model.Bufa = this.box_bufaType.Text;
            model.BufaPost = this.box_bufaPost.Value;
            model.id = _id;

            var res = await _bll.SaveBufaItems(new[] { model });

            if (res.IsOk)
            {
                if (_id <=0)
                {
                    //刷新
                    var list = await _bll.QueryBufaItems();
                    _id = list.First().id;
                }
                MessageTip.ShowOk(this, "保存完成");
            }
            else
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
    }
}
